package com.neusoft.yiyang.medical.service.impl;

import com.neusoft.yiyang.common.enums.ApprovalStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.yiyang.common.entity.medical.CareLevelSuggestion;
import com.neusoft.yiyang.medical.mapper.CareLevelSuggestionMapper;
import com.neusoft.yiyang.medical.service.CareLevelSuggestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CareLevelSuggestionServiceImpl implements CareLevelSuggestionService {

    @Autowired
    private CareLevelSuggestionMapper careLevelSuggestionMapper;

    @Override
    public void createCareLevelSuggestion(CareLevelSuggestion careLevelSuggestion) {
        careLevelSuggestionMapper.insert(careLevelSuggestion);
    }

    @Override
    public List<CareLevelSuggestion> getAllCareLevelSuggestions() {
        return careLevelSuggestionMapper.selectList(null);
    }

    @Override
    public List<CareLevelSuggestion> getCareLevelSuggestionsByCustomerId(Integer customerId) {
        return careLevelSuggestionMapper.selectByCustomerId(customerId);
    }

    @Override
    public void updateCareLevelSuggestion(Integer customerId, Integer suggestedLevelId, Integer medicalStaffId) {
        QueryWrapper<CareLevelSuggestion> qw = new QueryWrapper<>();
        qw.eq("customer_id",customerId);
        CareLevelSuggestion careLevelSuggestion = careLevelSuggestionMapper.selectOne(qw);
        if (careLevelSuggestion != null) {
            careLevelSuggestion.setSuggestedLevelId(suggestedLevelId);
            careLevelSuggestion.setMedicalStaffId(medicalStaffId);
            careLevelSuggestion.setSuggestTime(new java.util.Date());
            careLevelSuggestionMapper.updateById(careLevelSuggestion);
        } else {
            CareLevelSuggestion newCareLevelSuggestion = new CareLevelSuggestion();
            newCareLevelSuggestion.setCustomerId(customerId);
            newCareLevelSuggestion.setSuggestedLevelId(suggestedLevelId);
            newCareLevelSuggestion.setMedicalStaffId(medicalStaffId);
            newCareLevelSuggestion.setSuggestTime(new java.util.Date());
            newCareLevelSuggestion.setApprovalStatus(ApprovalStatus.pending);
            careLevelSuggestionMapper.insert(newCareLevelSuggestion);
        }

    }

    @Override
    public void deleteCareLevelSuggestion(Integer suggestionId) {
        careLevelSuggestionMapper.deleteById(suggestionId);
    }
}
